package com.lxmes.business.service.impl;

import java.util.Date;
import java.util.List;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.lxmes.business.mapper.ZcTemporaryPurchasePlanMapper;
import com.lxmes.business.domain.ZcTemporaryPurchasePlan;
import com.lxmes.business.service.IZcTemporaryPurchasePlanService;

/**
 * 临时采购计划Service业务层处理
 * 
 * @author lx
 * @date 2023-06-27
 */
@Service
public class ZcTemporaryPurchasePlanServiceImpl implements IZcTemporaryPurchasePlanService 
{
    @Autowired
    private ZcTemporaryPurchasePlanMapper zcTemporaryPurchasePlanMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询临时采购计划
     * 
     * @param id 临时采购计划主键
     * @return 临时采购计划
     */
    @Override
    public ZcTemporaryPurchasePlan selectZcTemporaryPurchasePlanById(Long id)
    {
        return zcTemporaryPurchasePlanMapper.selectZcTemporaryPurchasePlanById(id);
    }

    /**
     * 查询临时采购计划列表
     * 
     * @param zcTemporaryPurchasePlan 临时采购计划
     * @return 临时采购计划
     */
    @Override
    public List<ZcTemporaryPurchasePlan> selectZcTemporaryPurchasePlanList(ZcTemporaryPurchasePlan zcTemporaryPurchasePlan)
    {
        zcTemporaryPurchasePlan.setCompanyCode(SecurityUtils.getFactoryCode());
        return zcTemporaryPurchasePlanMapper.selectZcTemporaryPurchasePlanList(zcTemporaryPurchasePlan);
    }

    /**
     * 新增临时采购计划
     * 
     * @param zcTemporaryPurchasePlan 临时采购计划
     * @return 结果
     */
    @Override
    public int insertZcTemporaryPurchasePlan(ZcTemporaryPurchasePlan zcTemporaryPurchasePlan)
    {
        zcTemporaryPurchasePlan.setCompanyCode(SecurityUtils.getFactoryCode());
        zcTemporaryPurchasePlan.setCompanyName(SecurityUtils.getFactoryName());
        zcTemporaryPurchasePlan.setCreateTime(DateUtils.getNowDate());
        zcTemporaryPurchasePlan.setApplicant(SecurityUtils.getUsername());
        zcTemporaryPurchasePlan.setApplicationDate(new Date());
        zcTemporaryPurchasePlan.setDeptId(SecurityUtils.getDeptId());
        return zcTemporaryPurchasePlanMapper.insertZcTemporaryPurchasePlan(zcTemporaryPurchasePlan);
    }

    /**
     * 修改临时采购计划
     * 
     * @param zcTemporaryPurchasePlan 临时采购计划
     * @return 结果
     */
    @Override
    public int updateZcTemporaryPurchasePlan(ZcTemporaryPurchasePlan zcTemporaryPurchasePlan)
    {
        zcTemporaryPurchasePlan.setUpdateTime(DateUtils.getNowDate());
        return zcTemporaryPurchasePlanMapper.updateZcTemporaryPurchasePlan(zcTemporaryPurchasePlan);
    }

    /**
     * 批量删除临时采购计划
     * 
     * @param ids 需要删除的临时采购计划主键
     * @return 结果
     */
    @Override
    public int deleteZcTemporaryPurchasePlanByIds(Long[] ids)
    {
        return zcTemporaryPurchasePlanMapper.deleteZcTemporaryPurchasePlanByIds(ids);
    }

    /**
     * 删除临时采购计划信息
     * 
     * @param id 临时采购计划主键
     * @return 结果
     */
    @Override
    public int deleteZcTemporaryPurchasePlanById(Long id)
    {
        return zcTemporaryPurchasePlanMapper.deleteZcTemporaryPurchasePlanById(id);
    }
    @Override
    public String imporIteminfo(List<ZcTemporaryPurchasePlan> zcTemporaryPurchasePlanList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(zcTemporaryPurchasePlanList) || zcTemporaryPurchasePlanList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ZcTemporaryPurchasePlan zcTemporaryPurchasePlan : zcTemporaryPurchasePlanList)
        {
            try
            {
                BeanValidators.validateWithException(validator, zcTemporaryPurchasePlan);
                zcTemporaryPurchasePlan.setCreateBy(operName);
                zcTemporaryPurchasePlan.setUpdateBy(operName);
                zcTemporaryPurchasePlan.setCompanyCode(SecurityUtils.getFactoryCode());
                zcTemporaryPurchasePlan.setCompanyName(SecurityUtils.getFactoryName());
                this.insertZcTemporaryPurchasePlan(zcTemporaryPurchasePlan);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
